package com.dazhao.animejaphome.home;

import android.content.Intent;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.arialyy.aria.core.Aria;
import com.dazhao.animejaphome.R;
import com.dazhao.animejaphome.myRxBus.OnBackPressedInterceptEvent;
import com.dazhao.animejaphome.myRxBus.OnMenuDeleteClickEvent;
import com.dazhao.animejaphome.myRxBus.OnMenuDownloadEvent;
import com.dazhao.animejaphome.myRxBus.RxBus;
import com.gyf.barlibrary.ImmersionBar;

import java.util.ArrayList;


/**
 * Created by Coder-pig on 2015/8/28 0028.
 */
public class HomeActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener,
        ViewPager.OnPageChangeListener {
    private static final String TAG = "HomeActivity";
    //UI Objects
    private TextView txt_topbar;
    private RadioGroup rg_tab_bar;
    private RadioButton rb_channel;
    private RadioButton rb_message;
    private RadioButton rb_better;
    private RadioButton rb_setting;
    private ViewPager vpager;

    private MyFragmentPagerAdapter mAdapter;

    //几个代表页面的常量
    public static final int PAGE_ONE = 0;
    public static final int PAGE_TWO = 1;
    public static final int PAGE_THREE = 2;
//    public static final int PAGE_FOUR = 3;

    //动态显示隐藏optionsMenu需要用到的变量
    private boolean optionMenuOn = true;  //标示是否要显示optionmenu
    private Menu aMenu;         //获取optionmenu




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        ImmersionBar.with(this).init();//使导航栏透明
        getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.touming));

        mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        bindViews();
        rb_channel.setChecked(true);

        closeOptionsMenu();
        closeContextMenu();

//        Log.e(TAG, "onCreate: ", );
        Log.w(TAG, "onCreate: w");
        Log.d(TAG, "onCreate: d");
        Log.d(TAG, "onCreate: d");
        Log.v(TAG,"v");




//        Aria.download(this).register();

    }


    private void bindViews() {
        txt_topbar = (TextView) findViewById(R.id.txt_topbar);
        rg_tab_bar = (RadioGroup) findViewById(R.id.rg_tab_bar);
        rb_channel = (RadioButton) findViewById(R.id.rb_channel);
        rb_message = (RadioButton) findViewById(R.id.rb_message);
        rb_better = (RadioButton) findViewById(R.id.rb_better);

        rg_tab_bar.setOnCheckedChangeListener(this);

        vpager = (ViewPager) findViewById(R.id.vpager);
        vpager.setAdapter(mAdapter);
        vpager.setCurrentItem(0);
        vpager.addOnPageChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_channel:
                optionMenuOn=true;
                checkOptionMenu();
                vpager.setCurrentItem(PAGE_ONE);
                break;
            case R.id.rb_message:
                optionMenuOn=false;
                checkOptionMenu();
                vpager.setCurrentItem(PAGE_TWO);
                break;
            case R.id.rb_better:
                optionMenuOn=false;
                checkOptionMenu();
                vpager.setCurrentItem(PAGE_THREE);
                break;
//            case R.id.rb_setting:
//                optionMenuOn=false;
//                checkOptionMenu();
//                vpager.setCurrentItem(PAGE_FOUR);
//                break;
        }
    }


    //重写ViewPager页面切换的处理方法
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        //state的状态有三个，0表示什么都没做，1正在滑动，2滑动完毕
        if (state == 2) {
            switch (vpager.getCurrentItem()) {
                case PAGE_ONE:
                    rb_channel.setChecked(true);
                    break;
                case PAGE_TWO:
                    rb_message.setChecked(true);
                    break;
                case PAGE_THREE:
                    rb_better.setChecked(true);
                    break;
//                case PAGE_FOUR:
//                    rb_setting.setChecked(true);
//                    break;
            }
        }
    }

    //填充菜单项
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.my_menu, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        aMenu = menu;
        checkOptionMenu();
        return super.onPrepareOptionsMenu(menu);
    }
    public void checkOptionMenu(){
        if(null != aMenu){
            if(optionMenuOn){
                for (int i = 0; i < aMenu.size(); i++){
                    aMenu.getItem(i).setVisible(true);
                    aMenu.getItem(i).setEnabled(true);
                }
            }else{
                for (int i = 0; i < aMenu.size(); i++){
                    aMenu.getItem(i).setVisible(false);
                    aMenu.getItem(i).setEnabled(false);
                }
            }
        }
    }



    //菜单项的监听
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.menu_delete) {
            vpager.setCurrentItem(PAGE_ONE);//该菜单子项只为了第一个fragment使用




            RxBus.getInstance().send(new OnMenuDeleteClickEvent());

            return true;
        }
        if (id == R.id.menu_version_information) {
            startActivity(new Intent(this,VersionActivity.class));
            return true;
        }
//        if (id == R.id.menu_soso) {
//            return  true;
//        }
        if (id == R.id.menu_update) {//更新资源的方法
            vpager.setCurrentItem(PAGE_ONE);//该菜单子项只为了第一个fragment使用
            RxBus.getInstance().send(new OnMenuDownloadEvent());
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        if(vpager.getCurrentItem()==PAGE_ONE){
            MyFragment1Anime myFragment1=(MyFragment1Anime)mAdapter.getItem(0);
            LinearLayout bottom=myFragment1.getView().findViewById(R.id.main_bottom);
            if(bottom.getVisibility()== View.VISIBLE){
                RxBus.getInstance().send(new OnBackPressedInterceptEvent());
                return;
            }
        }

        super.onBackPressed();
    }

    public ViewPager getViewPager(){
        return vpager;
    }

    public void useDownloadFuntion(ArrayList<String> urls, String groupDirPath, Long fileSize){

        Toast.makeText(this, "调用了方法useDownloadFuntion", Toast.LENGTH_SHORT).show();
        Aria.download(this)
                .load(urls)
                .setDownloadDirPath(groupDirPath)    //设置任务组的文件夹路径
                /**
                 * 任务组总任务大小，任务组是一个抽象的概念，没有真实的数据实体，任务组的大小是Aria动态获取子任务大小相加而得到的，
                 * 如果你知道当前任务组总大小，你也可以调用该方法给任务组设置大小
                 *
                 * 为了更好的用户体验，建议直接设置任务组文件大小
                 */
                .setFileSize(fileSize)
                .start();   //启动下载
    }
}
